import { HttpException, HttpStatus } from "@nestjs/common";
import { Client } from "src/modules/client/client.entity";
import { Company } from "src/modules/company/company.entity";
import { Delivery } from "src/modules/delivery/delivery.entity";
import { Order } from "src/modules/order/order.entity";
import { Quality } from "src/modules/quality/quality.entity";
import { Statistic } from "src/modules/statistics/statistic.entity";
import { Type } from "src/modules/type/type.entity";

export const dateNewFirst = (a: { date: Date }, b: { date: Date }) =>
  b.date.getTime() - a.date.getTime();

export const getTimeWithoutMiniSecond = (): Date => {
  const d = new Date();
  d.setMilliseconds(0);
  return d;
};

export function itemCompanyCheck(
  company: Company,
  target:
    | (Type | Quality | Client | Order | Delivery | Statistic)[]
    | (Type | Quality | Client | Order | Delivery | Statistic)
) {
  if (!Array.isArray(target)) {
    target = [target];
  }
  const forbiddenResource = target.filter(
    (item) => item.company.id !== company.id
  );
  if (forbiddenResource.length > 0) {
    throw new HttpException(
      {
        message: "访问了不属于该公司的对象",
        targetIds: forbiddenResource.map((item) => item.id),
        targets: forbiddenResource,
        statusCode: HttpStatus.FORBIDDEN,
      },
      HttpStatus.FORBIDDEN
    );
  }
}
